<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'商品列表'),
	array('op'=>'edit','title'=>'添加商品'),	
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_zhongyi_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$member;
			$item['url']=custom_url('jy_zhongyi/zhuanke/detail/id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'商品ID','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'url','title'=>'商品链接','type'=>'copy'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'id','width'=>15,'title'=>'商品ID','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'商品标题','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['isfen']=array('不支持','支持');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_zhongyi_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3);
	}
	$item['videos']=$item['videos']?json_decode($item['videos'],true):array();
	$item['commissions']=json_decode($item['commissions'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit'=>array(
					array('name'=>'title','title'=>'商品名称','type'=>'string'),
					array('name'=>'price','title'=>'价格','type'=>'string'),
					array('name'=>'image','title'=>'商品封面','type'=>'image','help'=>'留空会自动截取商品第一张图片/图集第一张'),
					array('name'=>'desc','title'=>'商品介绍','type'=>'text'),
					array('name'=>'price','title'=>'商品价格','type'=>'string','show'=>array('type'=>1)),
					array('type'=>'richtext','title'=>'商品内容','name'=>'content' , 'help'=>''),
					//array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写商品名称！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),	
		'desc'=>trim($postdata['desc']),		
		'content'=>trim($postdata['content']),
		//'status'=>intval($postdata['status']),
		'price'=>trim($postdata['price']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_zhongyi_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_zhongyi_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_zhongyi_goods',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_zhongyi_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}